package xcmg.device.service.threeGuaranteesPur;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.threeGuaranteesPur.ThreeReverseDO;
import xcmg.device.dao.entity.threeGuaranteesPur.ThreeReverseDetailDO;
import xcmg.device.dao.mapper.threeGuaranteesPur.ThreeReverseDetailMapper;
import xcmg.device.dao.mapper.threeGuaranteesPur.ThreeReverseMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.threeGuaranteesPur.ThreeReverseDetailVO;
import xcmg.device.service.vo.threeGuaranteesPur.ThreeReverseVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ThreeReverseDetailService {

    @Autowired
    private ThreeReverseDetailMapper threeReverseDetailMapper;

    @Autowired
    private ThreeReverseMapper threeReverseMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * @Author: WangKe
     * @Description: 新增冲销入库详情记录
     * @Date: 2018/3/6 0006
     */
    public ErrorCode add(ThreeReverseDetailVO ThreeReverseDetailVO){
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        ThreeReverseDetailDO ThreeReverseDetailDO = new ThreeReverseDetailDO();
        BeanUtils.copyProperties(ThreeReverseDetailVO, ThreeReverseDetailDO);

        ThreeReverseDetailDO.setId(textResult.getValue());
        ThreeReverseDetailDO.setState(0);
        ThreeReverseDetailDO.setCreateDate(new Date());
        ThreeReverseDetailDO.setRealAmount(BigDecimal.ZERO);

        ErrorCode code = threeReverseDetailMapper.insert(ThreeReverseDetailDO)>0? ErrorCode.Success: ErrorCode.Failure;
        return code;
    }

    /**
     * @Author: WangKe
     * @Description: 新增入库记录时，更新冲销入库单和详情的状态
     * @Date: 2018/3/7 0007
     */
    public boolean updateByInbound(String reverseOrder, String materialNo, BigDecimal realAmount,
                                   Integer singelState, Integer totalState, String orgId)throws Exception{
        ThreeReverseDetailVO detailVO = new ThreeReverseDetailVO();
        detailVO.setReverseOrder(reverseOrder);
        detailVO.setMaterialNo(materialNo);
        detailVO.setOrgId(orgId);
        List<ThreeReverseDetailDO> list = threeReverseDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return false;
        }
        ThreeReverseDetailDO detailDO = list.get(0);
        if(realAmount.compareTo(detailDO.getAmount()) > 0){
            throw new Exception("实际入库数量不能大于计划入库数量");
        }
        detailDO.setRealAmount(realAmount);
        detailDO.setState(singelState);
        boolean success = threeReverseDetailMapper.update(detailDO)>0;
        if(success){
            ThreeReverseVO inboundVO = new ThreeReverseVO();
            inboundVO.setReverseOrder(reverseOrder);
            inboundVO.setOrgId(orgId);
            List<ThreeReverseDO> inboundList = threeReverseMapper.list(inboundVO);
            if(ListUtil.isNullOrEmpty(inboundList)){
                return false;
            }
            ThreeReverseDO inboundDO = inboundList.get(0);
            inboundDO.setState(totalState);
            success = threeReverseMapper.update(inboundDO)>0;
        }
        return success;
    }
}
